(* Module BoarderProcess : permet de détecter et marquer les contours de l'image en noir *)

let transform img =
  (*Transforme l'image de type OImage.rgb24 en int array array *)
  let w, h = (img#width, img#height) in
  let img_mat = (Array.make_matrix h w 0) in
    for i = 0 to (h - 1) do
      for j = 0 to (w - 1) do
        let pixel = img#get j i in
          img_mat.(i).(j) <- (Graphics.rgb pixel.Images.r pixel.Images.g pixel.Images.b)
      done;
    done;
    img_mat

let border_h image_mat =
  (* Parcours l'image dans le sens horizontal pour dessiner les contours *)
  let w, h = (Array.length image_mat,Array.length image_mat.(0)) in
  let new_mat =(Array.make_matrix w h Graphics.white) in
    for i = 0 to (w-1) do
      for j = 0 to (h-1) do
        let prec_color =
          image_mat.(if j=0 then if i=0 then 0 else i-1 else i).
            (if j=0 then if i=0 then 0 else h-1 else j-1)
   (*Si le pixel precedent est different du pixel actuel, le pixel actuel devient noir *)
	in
        (if prec_color <> image_mat.(i).(j) then
          new_mat.(i).(j) <- Graphics.black);
      done;
    done;
    new_mat


(* Parcours l'image dans le sens vertical pour dessiner les contours *)
let border_w image_mat =
  let w, h = (Array.length image_mat,Array.length image_mat.(0)) in
  let new_mat =(Array.make_matrix w h Graphics.white) in
    for i = 0 to (h-1) do
      for j = 0 to (w-1) do
        let prec_color =
          image_mat.(if j=0 then if i=0 then 0 else w-1 else j-1)
            .(if j=0 then if i=0 then 0 else i-1 else i)
        in
          (if prec_color <> image_mat.(j).(i) then
             new_mat.(j).(i) <- Graphics.black);
      done;
    done;
    new_mat

(* Réuni par un OU logique les deux traitements vertical et horizontal *)
let border_f image_w image_h =
  let w, h = (Array.length image_w, Array.length image_w.(0)) in
  let new_mat = (Array.make_matrix w h Graphics.white) in
    for i = 0  to (w-1) do
      for j = 0 to (h-1) do
        if image_w.(i).(j) = Graphics.black ||
            image_h.(i).(j) = Graphics.black then
          new_mat.(i).(j) <- Graphics.black else
            new_mat.(i).(j) <- Graphics.white;
      done;
    done;
new_mat


(* Réuni par un OU logique les deux traitements vertical et horizontal *)
(* applique le traitement des contours sur l'image colorée pour renvoyer le résultat final *)
let border_f image_base image_w image_h =
  let w, h = (Array.length image_base, Array.length image_base.(0)) in
    for i = 0  to (w-1) do
      for j = 0 to (h-1) do
        if image_w.(i).(j) = Graphics.black ||
          image_h.(i).(j) = Graphics.black then
            image_base.(i).(j) <- Graphics.black else
              ();
      done;
    done;
image_base


(*EOF*)
